package com.huike.clues.service.impl;

import com.huike.clues.domain.Course;
import com.huike.clues.mapper.CoursesMapper;
import com.huike.clues.service.CoursesService;
import com.huike.common.utils.SecurityUtils;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;
import java.util.Random;


@Service
public class CoursesServiceImpl implements CoursesService {

    @Autowired
    private CoursesMapper coursesMapper;
    //查询课程管理系统
    @Override
    public List<Course> selectCourseByPage(Course course) {
        return coursesMapper.selectCourseByPage(course);
    }
    //新增课程管理
    @Override
    public int addCourse(Course course) {
        Random random = new Random();
        //0到999999之间的任意数
        int number = 100000+random.nextInt(900000);
        String code = "kcgl"+number;
        course.setCode(code);
        Date now = Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant());
        course.setCreateTime(now);
        course.setUpdateTime(now);
        course.setCreateBy(SecurityUtils.getUsername());
        course.setUpdateBy(SecurityUtils.getUsername());
        int count = coursesMapper.addCourse(course);
        return count;
    }
    //修改课程管理系统
    @Override
    public int updateCourse(Course course) {
        Date now = Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant());
        course.setUpdateTime(now);
        course.setUpdateBy(SecurityUtils.getUsername());
        int count = coursesMapper.updateTime(course);
        return count;
    }
    //根据id查询课程管理信息
    @Override
    public Course selectById(Long id) {
        return coursesMapper.selectById(id);
    }
    //根据id数组删除
    @Override
    public void deleteByIds(Long[] ids) {
        for (Long id : ids) {
            coursesMapper.delectById(id);
        }

    }

    @Override
    public List<Course> listselect(Integer subject) {



        return coursesMapper.listselect(subject);
    }


}
